<template>
    <div class="video-list" v-loading="loading">
        <div v-show="!isDetail">
            <div class="input-box">
                <el-input
                    placeholder="关键字"
                    style="width:150px;"
                    @keyup.enter.native="getList(1)"
                    clearable
                    v-model.trim="keyWord">
                    <i slot="prefix" class="el-input__icon el-icon-search"></i>
                </el-input>
                <el-date-picker
                    @change="getList(1)"
                    v-model="date"
                    style="margin-left:10px;"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
                <el-select
                    @change="getList(1)"
                    style="margin-left:10px;width:100px;"
                    v-model="collectSource">
                    <el-option label="全部" :value="2"></el-option>
                    <el-option label="微博" :value="0"></el-option>
                    <el-option label="taptap" :value="1"></el-option>
                </el-select>
                <el-select
                    @change="getList(1)"
                    style="margin-left:10px;width:100px;"
                    v-model="status">
                    <el-option label="全部" :value="2"></el-option>
                    <el-option label="已采用" :value="0"></el-option>
                    <el-option label="待审核" :value="1"></el-option>
                </el-select>
                <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
            </div>
            <div class="table-box">
                <el-table
                    :data="tableData"
                    border
                    style="width: 100%">
                    <el-table-column
                        fixed
                        width="60"
                        prop="id"
                        label="id">
                    </el-table-column>
                    <el-table-column
                        prop="collectSource"
                        label="采集源">
                        <template slot-scope="scope">
                            <span v-if="scope.row.collectSource === '1'">taptap</span>
                            <span v-else>微博</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        width="180"
                        prop="videoTitle"
                        label="视频标题">
                    </el-table-column>
                    <el-table-column
                        width="180"
                        label="视频">
                        <template slot-scope="scope">
                            <videoBox v-if="scope.row.videoUrl" :url="scope.row.videoUrl" :time="scope.row.videoTime" :img="scope.row.videoCover"/>
                            <div v-else>暂无视频</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        width="110"
                        label="视频封面">
                        <template slot-scope="scope">
                            <div class="cover-box">
                                <img :src="scope.row.videoCover" alt="">
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        width="150"
                        label="作者">
                        <template slot-scope="scope">
                            <div>{{scope.row.userName || '/'}}</div>
                            <div>原：{{scope.row.publishUserName || '/'}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        width="110"
                        label="时间">
                        <template slot-scope="scope">
                            <div style="text-align:center;">{{scope.row.createTime | timeNo0}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        width="150"
                        label="相关游戏">
                        <template slot-scope="scope">
                            <span>{{scope.row.aboutGameName || '/'}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="right"
                        label="采用状态">
                        <template slot-scope="scope">
                            <span v-if="scope.row.status === '0'">已采用</span>
                            <span v-else style="color: #F56C6C;">待审核</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="right"
                        label="操作">
                        <template slot-scope="scope">
                            <div><el-button @click="up(scope.row)" type="text" size="small" v-if="scope.row.status === '1'" style="color:#67C23A;">上 架</el-button></div>
                            <div><el-button @click="goDetail(scope.row)" type="text" size="small">查看详情</el-button></div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="tools">
                <el-pagination
                    @size-change="sizeChange"
                    @current-change="pageChange"
                    :current-page="pageNum"
                    :page-sizes="_pageSizes"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </div>
        <div v-show="isDetail">
            <auditCollectVideoDetail v-if="isDetail" :data="currentItem" :userList="userList" :videoTypeList="videoTypeList" @close="isDetail = false"/>
        </div>
    </div>
</template>

<script>
import auditCollectVideoDetail from './auditCollectVideoDetail'
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            isDetail: false,
            keyWord: '', // 关键字
            collectSource: 2, // 采集源 0为微博 1为taptap
            status: 1, // 任务状态 0为运行 1为暂停
            date: [],
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            userList: [],
            currentItem: {},
            videoTypeList: []
        }
    },

    created () {
        this.getList(1)
        this.selectGameUser()
        this.getVideoTypeList()
    },

    methods: {
        // 获取列表
        async getList (num) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num
            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize
            }
            if (this.date && this.date.length) {
                params.fromDate = this.date[0]
                params.toDate = this.date[1]
            }
            if (this.keyWord) {
                params.keyWord = this.keyWord
            }
            // 采集源 0为微博 1为taptap
            if (this.collectSource !== 2) {
                params.collectSource = this.collectSource
            }
            // 任务状态 0为运行 1为暂停
            if (this.status !== 2) {
                params.status = this.status
            }
            let res = await this.$http.get('/spider/selectCollectVideo', { params: params })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        async selectGameUser () {
            let res = await this.$http.get('/spider/selectGameUser')
            if (res.code === '0') {
                this.userList = res.data
            }
        },

        async getVideoTypeList () {
            let res = await this.$http.post('/video/findVideoCategory')
            this.videoTypeList = res.data || []
        },

        async up (item) {
            let res = await this.$http.get('/spider/up', {
                params: {
                    videoId: item.id
                }
            })
            if (res.code === '0') {
                item.status = '1'
                this.$message({
                    type: 'success',
                    message: '上架成功'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        },

        // 页码切换
        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        // 页容切换
        pageChange (val) {
            this.getList(val)
        },

        _formatDate (timestamp, type = 'YYYY-MM-DD HH:MM') {
            // return timestamp
            return formatDate(timestamp, type)
        },

        goDetail (item) {
            this.currentItem = item
            this.isDetail = true
        }
    },

    components: {
        auditCollectVideoDetail
    }
}
</script>

<style lang="scss" scoped>
    .video-list {
        .input-box {
            .btn {
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
            .cover-box {
                display: flex;
                align-items: center;
                justify-content: center;
                > img {
                    width: 100px;
                    max-height: 100%;
                }
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
    .editor-dialog {
        /deep/ .el-dialog__body {
            padding: 0 20px;
        }
    }
</style>
